﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;


namespace MyHealthByDesign.Domain.Panels
{
    public class SimpleCheckBox : IQuestionPanel
    {
        #region IQuestionPanel Members

        public Panel GenerateQuestionDisplay(Question q)
        {
            Panel pnlIn = new Panel();

            Label lblC = new Label();
            lblC.Text = q.ContentLabel;
            lblC.CssClass = "panelLeft";

            Type typField = typeof(CheckBox).Assembly.GetType("System.Web.UI.WebControls.CheckBox");
            CheckBox cb = (CheckBox)Activator.CreateInstance(typField);
            cb.CssClass = q.FieldCSS;
            cb.ID = q.Name;
            
            pnlIn.Controls.Add(lblC);
            pnlIn.Controls.Add(cb);

            if (!string.IsNullOrEmpty(q.FieldSuffix))
            {
                Label lblSuffix = new Label();
                lblSuffix.Text = "&nbsp;&nbsp;" + q.FieldSuffix + "&nbsp;&nbsp;";
                pnlIn.Controls.Add(lblSuffix);
            }

            if (!string.IsNullOrEmpty(q.FieldHelpText) && !string.IsNullOrEmpty(q.FieldHelpURL))
            {
                HtmlGenericControl aHelp = new HtmlGenericControl("a");
                aHelp.Attributes.Add("href", q.FieldHelpURL);
                aHelp.Attributes.Add("target", "_blank");
                aHelp.InnerText = q.FieldHelpText;
                pnlIn.Controls.Add(aHelp);
            }

            return pnlIn;
        }

        
        public Dictionary<Question, NameValueCollection> RetrieveQuestionValue(Question q, ContentPlaceHolder cph)
        {
            Dictionary<Question, NameValueCollection> dFieldValues = new Dictionary<Question, NameValueCollection>();
            NameValueCollection nvc = new NameValueCollection();

            CheckBox cb = (CheckBox)cph.FindControl(q.Name);
            nvc.Add(q.Name, cb.Checked.ToString());

            dFieldValues.Add(q, nvc);

            return dFieldValues;
        }

        #endregion
    }
}